home *** CD-ROM | disk | FTP | other *** search
/ Ultra Pack / UltraComputing Partner Applications.iso / SunLabs / tclTK / src / tcl7.4 / compat / strtol.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-18  |  2.0 KB  |  86 lines

  1. /* 
  2.  * strtol.c --
  3.  *
  4.  *    Source code for the "strtol" library procedure.
  5.  *
  6.  * Copyright (c) 1988 The Regents of the University of California.
  7.  * Copyright (c) 1994 Sun Microsystems, Inc.
  8.  *
  9.  * See the file "license.terms" for information on usage and redistribution
  10.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11.  */
  12.  
  13. #ifndef lint
  14. static char sccsid[] = "@(#) strtol.c 1.3 94/12/17 16:26:25";
  15. #endif /* not lint */
  16.  
  17. #include <ctype.h>
  18.  
  19.  
  20. /*
  21.  *----------------------------------------------------------------------
  22.  *
  23.  * strtol --
  24.  *
  25.  *    Convert an ASCII string into an integer.
  26.  *
  27.  * Results:
  28.  *    The return value is the integer equivalent of string.  If endPtr
  29.  *    is non-NULL, then *endPtr is filled in with the character
  30.  *    after the last one that was part of the integer.  If string
  31.  *    doesn't contain a valid integer value, then zero is returned
  32.  *    and *endPtr is set to string.
  33.  *
  34.  * Side effects:
  35.  *    None.
  36.  *
  37.  *----------------------------------------------------------------------
  38.  */
  39.  
  40. long int
  41. strtol(string, endPtr, base)
  42.     char *string;        /* String of ASCII digits, possibly
  43.                  * preceded by white space.  For bases
  44.                  * greater than 10, either lower- or
  45.                  * upper-case digits may be used.
  46.                  */
  47.     char **endPtr;        /* Where to store address of terminating
  48.                  * character, or NULL. */
  49.     int base;            /* Base for conversion.  Must be less
  50.                  * than 37.  If 0, then the base is chosen
  51.                  * from the leading characters of string:
  52.                  * "0x" means hex, "0" means octal, anything
  53.                  * else means decimal.
  54.                  */
  55. {
  56.     register char *p;
  57.     int result;
  58.  
  59.     /*
  60.      * Skip any leading blanks.
  61.      */
  62.  
  63.     p = string;
  64.     while (isspace(*p)) {
  65.     p += 1;
  66.     }
  67.  
  68.     /*
  69.      * Check for a sign.
  70.      */
  71.  
  72.     if (*p == '-') {
  73.     p += 1;
  74.     result = -(strtoul(p, endPtr, base));
  75.     } else {
  76.     if (*p == '+') {
  77.         p += 1;
  78.     }
  79.     result = strtoul(p, endPtr, base);
  80.     }
  81.     if ((result == 0) && (endPtr != 0) && (*endPtr == p)) {
  82.     *endPtr = string;
  83.     }
  84.     return result;
  85. }
  86.